package cn.bob.org.controller;

import cn.bob.basic.config.BussinessException;
import cn.bob.basic.jwt.LoginContext;
import cn.bob.basic.utils.BaiduAuditUtils;
import cn.bob.basic.utils.ExcelUtil;
import cn.bob.org.domain.ShopAuditLog;
import cn.bob.org.service.IShopService;
import cn.bob.org.domain.Shop;
import cn.bob.org.query.ShopQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Service;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    //驳回
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog auditLog, HttpServletRequest request){
        try {
            if (auditLog == null) {
                return AjaxResult.me().fail();
            }
            //jwt获取到数据返回logininfo信息
            Logininfo logininfo = (Logininfo) LoginContext.getLogininfo(request);
            auditLog.setAuditId(logininfo.getId());
            shopService.reject(auditLog);

        } catch (BussinessException be) {
            return AjaxResult.me().fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().success();
    }

    //审核通过
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog auditLog,HttpServletRequest request){
        try {
            if (auditLog == null) {
                return AjaxResult.me().fail();
            }
            //jwt获取到数据返回logininfo信息
            Logininfo logininfo = (Logininfo) LoginContext.getLogininfo(request);
            auditLog.setAuditId(logininfo.getId());
            shopService.pass(auditLog);
        } catch (BussinessException be) {
            return AjaxResult.me().fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().success();
    }
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id){
        shopService.active(id);
        return AjaxResult.me().success();
    }

    /**
     * 接收前端的入驻信息将数据发送到后端
     * 进行保存到数据库
     */


    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {

        try {
            if (shop == null) {
                return AjaxResult.me().fail();
            }
            shopService.settlement(shop);
        } catch (BussinessException be) {
            return AjaxResult.me().fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().success();
    }

    //倍驳回后修改店铺时跳转的接口
    @PostMapping("/settlement/update")
    public AjaxResult setup(@RequestBody Shop shop) {

        try {
            if (shop == null) {
                return AjaxResult.me().fail();
            }
            shop.setState(0);
            shopService.update(shop);
        } catch (BussinessException be) {
            return AjaxResult.me().fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().success();
    }

    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addorup(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null) {
                shopService.update(shop);

            } else {
                shopService.addBaiDuAi(shop);
                shopService.add(shop);
            }
            return AjaxResult.me().success();
        } catch (BussinessException be) {
            return AjaxResult.me().fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.delById(id);
            return AjaxResult.me().success();
        } catch (BussinessException be) {
            return AjaxResult.me().fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Shop> list() {

        return shopService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {

        return shopService.queryByPage(query);
    }

    @GetMapping("/export/execl")
    public void exportExcel(HttpServletResponse response){
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,null,"店铺信息",Shop.class,"店铺信息.xlsx",response);
    }
    @PostMapping("/import/excel")
    public void exportExcel(@RequestPart("file")MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
//        for (Shop shop : shops) {
//            System.out.println(shop);
//        }
        //批量添加
        shopService.addAll(shops);
    }

    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        return  shopService.echarts();
    }

}
